package com.call.project.web;

import com.call.project.jni.CInterface;
import com.call.project.utils.OSinfo;
import com.call.project.web.controller.WebSocketController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * Created by IntelliJ IDEA.
 * User: wk
 * Date: 2017-03-06 上午10:28
 */
@SpringBootApplication(scanBasePackages = { "com.call.project.web", "com.call.project.task" })
@EnableScheduling
public class MySpringApplication {
    private Logger                         logger = LoggerFactory.getLogger(getClass());
    private ConfigurableApplicationContext configurableApplicationContext;

    public void run(String args[]) {
        SpringApplication springApplication = new SpringApplication(MySpringApplication.class);
        configurableApplicationContext = springApplication.run(args);
    }

    public void stop() {
        if (configurableApplicationContext != null && configurableApplicationContext.isRunning()) {
            configurableApplicationContext.close();
            configurableApplicationContext = null;
        }
    }

    public <T> T getBean(Class<T> clazz) {
        return configurableApplicationContext.getBean(clazz);
    }

    @Bean
    public CInterface cInterface(WebSocketController webSocketController) {
        CInterface cInterface = new CInterface();
        cInterface.setMsgCallBack(webSocketController);
        webSocketController.setCInteface(cInterface);
//        if (OSinfo.isWindows())
//            cInterface.registerWindows();
        return cInterface;
    }

    @Bean
    public FilterRegistrationBean frontFilterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setFilter(new FrontFilter());
        return filterRegistrationBean;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}
